/**
 * 
 */
package com.wbcs.jbsf.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jf.plugin.activerecord.Db;
import com.jf.plugin.activerecord.Record;

public class WbcsUtils {
	
	public static String[] getOriginalSelectSql(String sql){
		String[] sqls = new String[3];
		String presql = sql.substring(0, sql.indexOf("(")+1);
		String contentsql = sql.substring(presql.length(), sql.lastIndexOf(")"));
		String lastsql = sql.substring(contentsql.length());
		sqls[0] = presql; sqls[1] = contentsql; sqls[2] = lastsql;
		return sqls;
	}
	
	public static String wrapOriginalSelectSql(String[] sqls){
		if(sqls.length != 3)return null;
		return sqls[0] + sqls[1] +sqls[2];
	}
	
	public static int countMatches(String str, String subStr) {
        if ((str == null) || (str.length() == 0) || (subStr == null) || (subStr.length() == 0)) {
            return 0;
        }
        int count = 0;
        int index = 0;
        while ((index = str.indexOf(subStr, index)) != -1) {
            count++;
            index += subStr.length();
        }
        return count;
    }
	
	public static String parseString(String orgisql){
		//根据冒号确定多少个变量定义，确定循环次数
		int count = countMatches(orgisql,Operators.colon);
		String newstr = orgisql;
		String substr = orgisql;
		for (int i = 0; i < count; i++) {
			int num = i+1;
			substr = substr.substring(substr.indexOf(Operators.colon+num)+2);
			String operator = substr.substring(0, substr.indexOf(Operators.lbracket));
			String place = Operators.colon+num + substr.substring(0, substr.indexOf(Operators.rbracket))+Operators.rbracket;
			switch(Operators.valueOf(operator.trim())){
			case foror:
				//取出替换语句到内存
				String itemsql = substr.substring(substr.indexOf(Operators.lbracket)+1, substr.indexOf(Operators.rbracket));
				//获取数据来源
				String srcsubstr = substr.substring(substr.indexOf(Operators.lsrc+num)+3);
				String src = srcsubstr.substring(0, srcsubstr.indexOf(Operators.rsrc));
				String srcplace = (Operators.lsrc+num) + src + (Operators.rsrc);
				//删除来源占位
				newstr = newstr.replace(srcplace, "");
				//查询的得到数据集合
				List<Record> lst = Db.find(src.trim());
				//执行foror循环
				int variablecount = countMatches(itemsql, Operators.startvariable);
				StringBuffer wheresb = new StringBuffer();
				for (int j = 0; j < lst.size(); j++) {//得到条件
					String subitemsql = itemsql;
					String newitem = "";
					for (int v = 0; v < variablecount; v++) {
						System.out.println(subitemsql.indexOf(Operators.startvariable) + "    " + subitemsql.indexOf(Operators.endvariable));
						if(subitemsql.indexOf(Operators.startvariable) == -1 || subitemsql.indexOf(Operators.endvariable) == -1)break;
						Record r = lst.get(j);
						subitemsql = subitemsql.substring(subitemsql.indexOf(Operators.startvariable)+1);
						String colname = subitemsql.substring(0, subitemsql.indexOf(Operators.endvariable));
						subitemsql = subitemsql.substring(subitemsql.indexOf(Operators.endvariable)+1);
						String colval = r.getStr(colname.toUpperCase());
						newitem = itemsql.replace(Operators.startvariable+colname+Operators.endvariable, colval);
					}
					if(!"".equals(newitem))wheresb.append(newitem).append(" or ");
				}
				//删除替换语句
				newstr = newstr.replace(place, wheresb.substring(0, wheresb.lastIndexOf("or")));
				//替换占位符
				break;
			default:
				return null;
			}
		}
		return newstr;
	}
	
	public static String getYear(){
		Calendar cal=Calendar.getInstance();//使用日历类
        int year=cal.get(Calendar.YEAR);//得到年
        return String.valueOf(year);
	}
	
	public static String getMonth(){
		Calendar cal=Calendar.getInstance();//使用日历类
        int month=cal.get(Calendar.MONTH)+1;//得到月
        String prefix = "0", monthstr = "";
        if(month < 10){
        	monthstr = prefix + month;
        }else{
        	monthstr = String.valueOf(month);
        }
        return monthstr;
	}
	
	public static String getDefaultTime(){
		Date nowTime=new Date();
        SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        return time.format(nowTime);
	}
	
	public static void main(String[] args) {
		System.out.println(getMonth());
	}
}
